package com.megalith.controller;

import com.megalith.dto.MembershipDTO;
import com.megalith.dto.UserGroupDTO;
import com.megalith.service.ProcessEngineUserGroupService;
import com.megalith.utils.RequestResponseUtil;
import com.megalith.vo.Result;
import com.megalith.vo.UserGroupVO;
import com.megalith.vo.UserGroupWithUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 流程引擎用户组管理
 * @Author lfp
 * @Date 2022/7/15
 * @Version 1.0
 */
@RestController
@Api(tags = "流程引擎用户组管理")
@RequestMapping("group")
public class ProcessEngineUserGroupController {
    @Autowired
    private ProcessEngineUserGroupService processEngineUserGroupService;


    @ApiOperation(value = "添加用户组")
    @PostMapping("add")
    public Result<UserGroupVO> addGroup(@Validated @RequestBody UserGroupDTO groupDTO, BindingResult result){
        if (result.hasErrors()) {
            return RequestResponseUtil.doBindingResultHandel(result);
        }else {
            return processEngineUserGroupService.addGroup(groupDTO);
        }
    }



    @ApiOperation(value = "删除用户组")
    @DeleteMapping("delete/{groupId}")
    public Result deleteGroup(@PathVariable String groupId){
        return processEngineUserGroupService.deleteGroup(groupId);
    }

    @ApiOperation(value = "用户与用户组关联")
    @PostMapping("membership/create")
    public Result createMembership(@Validated @RequestBody MembershipDTO membershipDTO, BindingResult result){
        if (result.hasErrors()) {
            return RequestResponseUtil.doBindingResultHandel(result);
        }else {
            return processEngineUserGroupService.createMembership(membershipDTO);
        }
    }

    //todo 用户组 列表查询

    @ApiOperation(value = "用户组及关联用户查询")
    @GetMapping("user/list")
    public Result<List<UserGroupWithUserVO>> listUserGroupWithUser(UserGroupDTO groupDTO){
        return Result.ok(processEngineUserGroupService.listUserGroupWithUser(groupDTO));
    }
}
